package com.flame.user.controller;

import com.flame.auth.core.annotation.HasAuth;
import com.flame.core.protocol.PageResult;
import com.flame.core.protocol.Result;
import com.flame.user.controller.vo.RoleVo;
import com.flame.user.entity.Role;
import com.flame.user.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * flame_role 角色信息表
 *
 * @author 吴欣童
 * @since 2022-12-15 10:04:46
 */
@RestController
public class RoleController {

    @Autowired
    private RoleService roleService;

    @PostMapping("/role/list")
    public Result list(@RequestBody List<String> roleCodeList) {
        return Result.success(roleService.getRoleSimpleById(roleCodeList));
    }

    @HasAuth("role")
    @PostMapping("/role/page")
    public PageResult pageRole(@RequestBody RoleVo roleVo) {
        return PageResult.success(roleService.pageRole(roleVo));
    }

    @HasAuth("role:add")
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/role/add")
    public Result addRole(@RequestBody @Validated Role role) {
        roleService.addRole(role);
        return Result.success();
    }

    @HasAuth("role:edit")
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/role/update")
    public Result updateRole(@RequestBody @Validated Role role) {
        roleService.updateRole(role);
        return Result.success();
    }

    @HasAuth("role:delete")
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/role/delete")
    public Result deleteRole(@RequestBody @Validated Role role) {
        roleService.deleteRole(role);
        return Result.success();
    }
}
